#include "todoitem.h"

#include <QStringList>

TodoItem::TodoItem()
{
}

TodoItem::TodoItem(int priority, QString description, bool urgent, QDate date, bool complete)
{
    this->code = -1;
    this->urgent = urgent;
    this->description = description;
    this->priority = priority;
    this->date = date;
    this->complete = complete;
}

TodoItem::TodoItem(int code, int priority, QString description, bool urgent, QDate date, bool complete)
{
    this->code = code;
    this->urgent = urgent;
    this->description = description;
    this->priority = priority;
    this->date = date;
    this->complete = complete;
}

TodoItem::TodoItem(QString code, QString priority, QString description, QString urgent, QString date, QString complete)
{
    this->code = _stringToInt(code);
    this->urgent = _stringToBool(urgent);
    this->description = description;
    this->priority = _stringToPriority(priority);
    this->date = _stringToDate(date);
    this->complete = _stringToBool(complete);

}

bool TodoItem::_stringToBool(const QString &s) {
    QString yes = "YSTV";
    if( (!s.isEmpty()) && yes.contains(s, Qt::CaseInsensitive) ) {
        return true;
    }
    return false;
}

int TodoItem::_stringToInt(const QString &s) {
    QString buf = s.trimmed();
    if( buf.startsWith("(") ) {
        buf = buf.mid(1, buf.length()-2);
    }
    if( buf.startsWith("#") ) {
        buf = buf.mid(1);
    }
    return (buf.isEmpty()?-1:buf.toInt());
}

int TodoItem::_stringToPriority(const QString &s) {

    QString buf = s.trimmed();
    if( buf.startsWith("(") ) {
        buf = buf.mid(1, buf.length()-2);
    }

    if( buf.isEmpty() ) return 'Z';
    if( buf[0].isDigit() ) return buf.toInt();
    char c = buf[0].toUpper().toLatin1();
    return c - (char)'A';
}

QDate TodoItem::_stringToDate(const QString &s) {

    if( s.isEmpty() )
        return QDate::currentDate();

    if( s.indexOf('-') > 0 ) {
        return QDate::fromString(s, "yyyy-MM-dd");
    }

    if( s.indexOf('/') > 0 ) {
        return QDate::fromString(s, "dd/MM/yyyy");
    }

    return QDate::fromString(s, "ddMMyyyy");

}

/*
QString TodoItem::toString() {
    QStringList ss;
    //ss << QString::number(code) << (urgent?"S":"N") << description << QString::number(priority) << date.toString("dd/MM/yyyy") << (complete?"S":"N");
    ss << QString::number(code) << (urgent?"S":"N") << description << QString::number(priority) << date.toString("yyyy-MM-dd") << (complete?"S":"N");
    return ss.join("|");
}

QString TodoItem::toFormattedString() {

    QStringList ss;
    ss << QString::number(code) << "<font color='green'>(" + QString::number(priority) + ")</font>" << description << (urgent?"<font color='red'>S</font>":"N") << date.toString("dd/MM/yyyy") << (complete?"S":"N");
    return ss.join(" ");

}
*/

QString TodoItem::priorityToString()
{
    if( priority == -1 ) {
        return "";
    }
    char c = 'A' + priority;
    return QString(c);
}

QString TodoItem::dateToString()
{
    return date.toString("yyyy-MM-dd");
}
